package com.wanxi.dao.impl;

import com.wanxi.dao.ProductDao;
import com.wanxi.model.ProductModel;
import com.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "INSERT INTO `product` (" +
                "`name`, `normal_price`, `price`, " +
                "`content`, `is_show`, `is_recommend`, " +
                "`img_href`, `create_time`, `update_time`, " +
                "`enable`, `where_show`, `cuisine_id`) VALUES (" +
                "'" + productModel.getName() + "', '" + productModel.getNormalPrice() + "', '" + productModel.getMarketPrice() + "', " +
                "'" + productModel.getContent() + "', '1', '1', " +
                "'" + productModel.getImgHref() + "', now(), now(), " +
                "'1', '1', '" + productModel.getCuisineId() + "');\n";
        return JDBC.update(sql);
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "update set product ";
        if (!"".equals(productModel.getImgHref())) {
            sql += "img_href='" + productModel.getImgHref() + "',";
        }
        if (!"".equals(productModel.getName())) {
            sql += "name='" + productModel.getName() + "',";
        }
//        if (!"".equals(productModel.getImgHref())) {
//            sql += "img_href='" + productModel.getImgHref() + "',";
//        }
        sql += " update_time=now() ";
        sql += "where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public int delete(ProductModel productModel) {
        String sql = "delete from product where id=" + productModel.getId();
        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        String sql = "select * from product where id=" + productModel.getId();
        ResultSet query = JDBC.query(sql);
        ProductModel model = null;
        try {
            while (query.next()) {
                model = ProductModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .createTime(query.getString("create_time"))
                        .cuisineId(query.getInt("cuisine_id"))
                        .imgHref(query.getString("img_href"))
                        .enable(query.getInt("enable"))
                        .content(query.getString("content"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .build();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int count(ProductModel productModel) {
        String sql = " select count(*) count from ( " +
                " select p.*,c.name cuisineName from product p " +
                " left join cuisine c " +
                " on  p.cuisine_id=c.id " +
                " ) aa";

        return JDBC.count(sql);
    }

    @Override
    public List<ProductModel> findAll(ProductModel model) {
        String sql = " select * from ( " +
                " select p.*,c.name cuisineName from product p " +
                " left join cuisine c " +
                " on  p.cuisine_id=c.id " +
                " ) temp ";
//        if (!"".equals(productModel.getName())) {
//            sql += " and temp.name like '%" + productModel.getName() + "%'";
//        }

        sql += " limit " + (model.getPage() + -1) * model.getLimit() + "," + model.getLimit() + " ";
        List<ProductModel> list = new ArrayList<>();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
//                ProductModel model = new ProductModel();
//                model.setId(query.getInt("id"));
                ProductModel productModel = ProductModel.builder()
                        .id(query.getInt("id"))
                        .name(query.getString("name"))
                        .createTime(query.getString("create_time"))
                        .cuisineName(query.getString("cuisineName"))
                        .imgHref(query.getString("img_href"))
                        .enable(query.getInt("enable"))
                        .normalPrice(query.getBigDecimal("normal_price"))
                        .build();
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    public int enable(int id) {
        return 0;
    }
}
